package com.romadan.core.wolfram.gui;

import java.io.File;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class FarTree {

	public DefaultTreeModel getModel(TreePath arg, DialogWindow mainWindow) {
		if (arg == null)
			return null;
		DefaultTreeModel result = null;
		File dir = getFileName(arg);
		int countArg = arg.getPathCount();

		if (dir.getPath().equals("")) {
			result = initTree();
		} else {
			DefaultMutableTreeNode fileTree = new DefaultMutableTreeNode();
			mainWindow.setRequiredFile(dir);
			if (dir.isFile()) {
				dir = dir.getParentFile();
			}
			fileTree = getModelOfDir(dir);
			if (countArg != 1)
				fileTree.insert(new DefaultMutableTreeNode(".."), 0);
			result = new DefaultTreeModel(fileTree, false);
		}
		return result;
	}

	private DefaultMutableTreeNode getModelOfDir(File dir) {
		DefaultMutableTreeNode result = new DefaultMutableTreeNode(dir);
		CatalogFiles files = getCatalogInDir(dir);
		for (File f : files.getDirs()) {
			result.add(new DefaultMutableTreeNode(f.getName()));
		}
		for (File f : files.getFiles()) {
			result.add(new DefaultMutableTreeNode(f.getName()));
		}
		return result;
	}

	private CatalogFiles getCatalogInDir(File dir) {
		CatalogFiles result = new CatalogFiles();
		try {
			String[] dirList = dir.list();
			for (int i = 0; i < dirList.length; i++) {
				File f = new File(dir.getAbsolutePath() + File.separator
						+ dirList[i]);
				if (!f.isFile()) {
					result.addDirs(f);
				} else {
					result.addFiles(f);
				}
			}
		} catch (NullPointerException e) {
		}
		return result;
	}

	private File getFileName(TreePath arg) {
		if (arg == null)
			return null;
		String sRes = "";
		String fname = arg.getLastPathComponent().toString();

		if (fname.equalsIgnoreCase("..")) {
			sRes = arg.getPathComponent(0).toString();
			if (isRoot(sRes)) {
				sRes = "";
			} else {
				while (!sRes.endsWith(File.separator)) {
					sRes = sRes.substring(0, sRes.length() - 1);
				}
			}
		} else {
			for (int i = 0; i < arg.getPathCount(); i++) {
				sRes += arg.getPathComponent(i) + File.separator;
			}
		}
		return new File(sRes);
	}

	private File[] getRoots() {
		return File.listRoots();
	}

	private boolean isRoot(File fDir) {
		// Check if work directory is root.
		boolean bDirIsRoot = false;
		File[] arrayRoots = File.listRoots();
		for (int i = 0; i < arrayRoots.length; i++) {
			if (arrayRoots[i].equals(fDir))
				bDirIsRoot = true;
		}
		return bDirIsRoot;
	}

	private boolean isRoot(String dir) {
		return isRoot(new File(dir));
	}

	public DefaultTreeModel initTree() {
		File[] roots = getRoots();
		DefaultMutableTreeNode node = new DefaultMutableTreeNode("");
		for (int i = 0; i < roots.length; i++) {
			node.insert(new DefaultMutableTreeNode(roots[i].getPath()), i);
		}
		DefaultTreeModel model = new DefaultTreeModel(node, false);
		return model;
	}
}
